/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.QuickTips.init();
var _pageSize = 20;

var fieldName = ['id', 'deviceNo', 'modelId', 'adminAreaCode', 'installSite', 'mobileNo', 'uniqueCode', 'pointId', 'pointName', 'runTime', 'checkTime', 'expireTime', 'startStatusCh','reportStatusCh',
    'note', 'updateTime', 'createTime', 'orgCode', 'model', 'commTypeName', 'manufactoryName', 'produceTime', 'channel', 'zbm', 'company', 'subCompany', 'shortName',
    'pointType', 'pointType2', 'pointName', 'terminalNo', 'reportNo', 'checkResult', 'voltage', 'lowerLimit', 'upperLimit', 'ip', 'port'];
var fieldLabel = ['ID', '设备编号', '终端型号ID', '区域码', '安装地点', '手机号码', '唯一码', '监测点ID', '监测点名称', '投运日期', '检测日期','有效期', '状态',
    '备注', '更新日期', '创建日期', '机构编码', '型号', '通信方式', '生产厂家', '出厂日期', '通道', '组编码', '供电局', '部门分县局', '供电所',
    '监测点类型', '电网类型', '监测点名称', '终端编号', '报告编号', '检验结果', '电压等级', '电压下限', '电压上限', 'IP地址', '端口'];

var saveServlet = basePath + 'servlet/system/DeviceSave';
var loadServlet = basePath + 'servlet/system/DeviceLoad';

var theme = '装置校验';

var _title = currentOrgName + theme + '管理';

var now = new Date();
var dayFrom = now.add(Date.DAY, -10);

var checkForm = new Ext.FormPanel(
        {
            xtype: 'form',
            id: 'CheckForm',
            bodyStyle: 'padding:15px;background:transparent',
            frame: true,
            border: false,
            url: saveServlet,
            items: [{
                    name: fieldName[0],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[0],
                    hidden: true
                }
                , {
                    name: fieldName[1],
                    xtype: 'textfield',
                    fieldLabel: fieldLabel[1],
                    allowBlank: false,
                    readOnly: true,
                    disable: true
                },
                {
                    name: 'model',
                    xtype: 'textfield',
                    readOnly: true,
                    fieldLabel: "型号",
                    disable: true
                },
                {
                    name: 'manufactoryName',
                    xtype: 'textfield',
                    readOnly: true,
                    fieldLabel: "生产厂家",
                    disable: true
                },
                {
                    name: 'pointName',
                    xtype: 'textfield',
                    id: 'PointName',
                    readOnly: true,
                    fieldLabel: "监测点",
                    disable: true
                }, {
                    name: 'checkTime',
                    xtype: 'datefield',
                    fieldLabel: '上次检验日期',
                    format: 'Y-m-d',
                    hidden: true,
                    disable: true
                }, {
                    name: 'expireTime',
                    xtype: 'datefield',
                    fieldLabel: '上次有效期',
                    format: 'Y-m-d',
                    hidden: true,
                    disable: true
                }, {
                    name: 'checkTimeNew',
                    xtype: 'datefield',
                    fieldLabel: '新检验日期',
                    format: 'Y-m-d h:i:s',
                    value: new Date()
                }, {
                    name: 'expireTimeNew',
                    xtype: 'datefield',
                    fieldLabel: '新有效期',
                    format: 'Y-m-d h:i:s',
                    value: new Date().add("year", 1)
                },
                {
                    name: 'operator',
                    xtype: 'textfield',
                    id: 'Operator',
                    readOnly: true,
                    fieldLabel: "检验人",
                    value: realName
                },
                {
                    name: 'reportNo',
                    xtype: 'textfield',
                    id: 'ReportNo',
                    fieldLabel: "报告编号"
                }
                ,
                {
                    name: 'checkResult',
                    xtype: 'textfield',
                    id: 'CheckResult',
                    fieldLabel: "检验结果"
                }
            ],
            buttons: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function() {
                        var submitConfig = {
                            success: function(form, action) {
                                store.reload();
                                winCheck.hide();
                            },
                            failure: function(form, action) {
                                switch (action.failureType) {
                                    case Ext.form.Action.CONNECT_FAILURE:
                                        Ext.Msg.alert('提示', '网络连接失败');
                                        break;
                                    case Ext.form.Action.SERVER_INVALID:
                                        Ext.Msg.alert('提示', action.result.msg);
                                }
                            },
                            params: {}
                        };
                        //submitConfig.params.id = checkboxSel.getSelected().get("id");
                        submitConfig.params.cmd = 'check';
                        Ext.getCmp('CheckForm').getForm().submit(submitConfig);
                    }
                }, {
                    text: '取消',
                    handler: function() {
                        winCheck.hide();
                    }
                }]
        }
);
var winCheck = new Ext.Window({
    title: '装置校验',
    layout: 'form',
    width: 340,
    autoHeight: true,
    closeAction: 'hide',
    items: [checkForm]
});



function check() {
    //Ext.getCmp('CheckForm').getForm().reset();
    var r = checkboxSel.getSelected();
    Ext.getCmp('CheckForm').getForm().loadRecord(r);
    var pointName = r.get('pointName');
    if (pointName === null || pointName === '')
        Ext.getCmp('PointName').setValue('未绑定');
    winCheck.show();
}


var checkboxSel = new Ext.grid.CheckboxSelectionModel();
var store;
function buildWindow() {
    store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName
        , baseParams: {loadType: 'check', dayFrom: dayFrom.format('Y-m-d'), start: 0, limit: _pageSize}
        , autoLoad: true
    });

    var grid = new Ext.grid.GridPanel({
        title: _title,
        store: store,
        sm: checkboxSel,
        columns: [
            checkboxSel,
            {header: fieldLabel[0], sortable: true, dataIndex: fieldName[0], hidden: true},
            {header: '供电局', width: 100, sortable: true, dataIndex: 'company'},
            {header: '部门分县局', width: 100, sortable: true, dataIndex: 'subCompany'},
            {header: '供电所', width: 100, sortable: true, dataIndex: 'shortName'},
            {header: '监测点名称', width: 100, sortable: true, dataIndex: 'pointName'},
            {header: '终端编号', width: 80, sortable: true, dataIndex: 'terminalNo'},
            {header: '区域码', width: 100, sortable: true, dataIndex: 'adminAreaCode'},
            {header: '组编码', width: 100, sortable: true, dataIndex: 'zbm'},
            {header: '电压等级', width: 100, sortable: true, dataIndex: 'voltage', align: 'right'},
            {header: '电压上限', width: 100, sortable: true, dataIndex: 'upperLimit', xtype: 'numbercolumn', format: '0.00', align: 'right'},
            {header: '电压下限', width: 100, sortable: true, dataIndex: 'lowerLimit', xtype: 'numbercolumn', format: '0.00', align: 'right'},
            {header: '检测日期', width: 80, sortable: true, dataIndex: 'checkTime', xtype: 'datecolumn', format: 'Y-m-d'},
            {header: '有效期', width: 80, sortable: true, dataIndex: 'expireTime', xtype: 'datecolumn', format: 'Y-m-d',css:'color:orange;'},
            {header: '报告编号', width: 80, sortable: true, dataIndex: 'reportNo',hidden:true},
            {header: '检验结果', width: 100, sortable: true, dataIndex: 'result',hidden:true},
            {header: '监测点类型', width: 100, sortable: true, dataIndex: 'pointType'},
            {header: '电网类型', width: 100, sortable: true, dataIndex: 'pointType2'},
            {header: '安装地点', width: 80, sortable: true, dataIndex: 'installSite'},
            {header: 'IP地址', width: 120, sortable: true, dataIndex: 'ip'},
            {header: '端口', width: 80, sortable: true, dataIndex: 'port'},
            {header: '通信类型', width: 80, sortable: true, dataIndex: 'commTypeName'},
            {header: '设备编号', width: 100, sortable: true, dataIndex: 'deviceNo'},
            {header: '型号', width: 80, sortable: true, dataIndex: 'model'},
            {header: '生产厂家', width: 100, sortable: true, dataIndex: 'manufactoryName'},
            {header: '投运日期', width: 80, sortable: true, dataIndex: 'runTime', xtype: 'datecolumn', format: 'Y-m-d'},
            {header: '出厂日期', width: 80, sortable: true, dataIndex: 'produceTime', xtype: 'datecolumn', format: 'Y-m-d'},
            {header: '启用', width: 80, sortable: true, dataIndex: 'startStatusCh'},
            {header: '上报', width: 80, sortable: true, dataIndex: 'reportStatusCh'},
            {header: '备注', width: 80, sortable: true, dataIndex: 'note'}
        ],
        //autoExpandColumn: fieldName[2],
        //renderTo: Ext.getBody(),
        //width: 780,
        //height: 590,
        loadMask: true,
        tbar: new Ext.Toolbar([
            {text: '有效期起始日期：', xtype: 'label'},
            {
                xtype: 'datefield',
                id: 'DayFrom',
                value: dayFrom,
                format: 'Y年m月d日',
                width: 150,
                listeners: {
                    select: function(c, date) {
                        store.baseParams.dayFrom = date.format('Y-m-d');
                    }
                }
            },
            {text: '搜索', iconCls: 'icon-search', handler: function() {
                    store.reload();
                }},
            '-',
            {text: '校验', iconCls: 'icon-check', handler: check}
        ]),
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: "目前没有" + theme + '信息'
        })
    });

    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            grid
        ]
    });
}
Ext.onReady(buildWindow);
